<?php
namespace Home\Controller;

use Util\AccessController;

require_once(THINK_PATH . "Library/Util/WxSendMsg/SendMsg.php");

class BookGetController extends AccessController {
    /*
       * 根据书籍条形码查询书籍的列表
        */
    public function GetBook_ISBN() {
        $where['ISBN'] = $_POST['ISBN'];
        $field = C('HS_BOOK_LS');
        $LS = Service('M', 'book');
        $DATA = $LS->query($where, null, $field);
//        if(!$DATA) {
//            $apidata = GetBookToIsbnInDDAPI($_POST['ISBN']);
//            $apidata['IS_BLACK'] = -1;
//            $DATA[0] = $apidata;
//        }
        $res = ResData($DATA);
        $this->ajaxReturn($res, 'JSON');
    }

    /*
     * 生成收书订单
     */
    public function AddSBookOrder() {
        $datas = $_POST;
        $OID = 'WXHSOID_' . uniqid() . rand(10, 99);//订单ID
        $LS = Service('BookGet', 'order');
        $OK = $LS->AddOrder($datas, $OID);
        $this->AddOrder_SendMsg($OID, $datas['order_address_name'], $datas['get_time'], $datas['order_address'], $datas['order_phone']);
        $data['res'] = $OK;
        $data['oid'] = $OID;
        $DATA = ResData($data);
        $this->ajaxReturn($DATA, 'JSON');
    }

    /*
     * 查看用户回收订单列表
     * post
     * USER_ID 用户user_id
     * (选填)分页页数PAGE=1
     * (选填)分页每页显示数据量PAGE_NUM=10
     * (选填)订单类型STATE=1（1：下单成功2：人工初审3：安排取件4：顺丰取件 5:收货 6确认打款 7关闭 -1删除）
     */
    public function ShowHsOrderList() {
        $where['ts_order.user_id'] = $_POST['USER_ID'];
        $where['ts_order.order_leixing'] = 3;
        $_POST['STATE'] ? $where['ts_order.order_type'] = $_POST['STATE'] : $where['ts_order.order_type'] = array('neq',
            -1);
        $limit = GET_LIMIT($_POST['PAGE'], $_POST['PAGE_NUM']);
        $LS = Service('BookGet', 'order');
        $data = $LS->GetAllOrder($where, $limit);
        $DATA = ResData($data);
        $this->ajaxReturn($DATA, 'JSON');
    }

    /*
 * 获取订单详情
 * post
 * ORDER_ID  订单id
 */
    public function GetOrderData() {
        $where['order_id'] = $_POST['ORDER_ID'];
        $LS = Service('BookGet', 'order');
        $data = $LS->GetOrderData($where);
        $data['group_num'] = $LS->GroupOrderGetMoney($_POST['ORDER_ID']);
        $DATA = ResData($data);
        $this->ajaxReturn($DATA, 'JSON');
    }


    /*
* 修改订单状态接口（关闭  确认收货）
* post
* ORDER_ID  订单id
* TYPE （-1关闭  -2伪删除）
*/
    public function UpdateOrderState() {
        $where['ts_order.order_id'] = $_POST['ORDER_ID'];
        $LS = Service('BookGet', 'order');
        if ($_POST['TYPE'] == -1) {
            //取消订单，查看是否参团，是删除进团记录，如果是该团的最后一个则删除团
            $save['order_type'] = 6;
            $data = $LS->qxOrder($where, $save);
            //删除记录
            $LS_LOG = Service('M','order_log');
            $LOG_SAVE['ORDER_ID'] = $_POST['ORDER_ID'];
            $LOG_SAVE['CERATE_TIME'] = date('Y-m-d H:i:s');
            $LOG_SAVE['INFO'] = '前端接口操作取消订单';
            $LS_LOG->Add($LOG_SAVE);
            $DATA = ResData($data);
            $this->ajaxReturn($DATA, 'JSON');
        } else if ($_POST['TYPE'] == -2) {
            $save['order_type'] = -1;
            $data = $LS->update($where, $save);
            $DATA = ResData($data);
            $this->ajaxReturn($DATA, 'JSON');
        } else {
            $DATA = ResErrorData();
            $this->ajaxReturn($DATA, 'JSON');
        }

    }

    /*
     * 修改上门时间
     */
    public function UpdateGetTime() {
        $where['ts_order.order_id'] = $_POST['ORDER_ID'];
        $LS = Service('BookGet', 'order');
        $save['order_get_time'] = $_POST['order_get_time'];
        $data = $LS->update($where, $save);
        $DATA = ResData($data);
        $this->ajaxReturn($DATA, 'JSON');
    }

    /*
     * 平台没有此书籍加入 审核表
     * OK 1 插入成功
     * OK 2 同个用户插入多次
     * OK 0 插入失败
     */
    public function SH_BOOK() {
        $save['OPENID'] = passport_decrypt($_POST['token'], C('JM_KEY'));
        $save['ISBN'] = $_POST['isbn'];
        $LS = Service('BookGet', 'sh_book');
        $findOK = $LS->find($save);
        $save['CREATETIME'] = date("Y-m-d H:i:s");
        if ($findOK) {
            $data['OK'] = 2;
        } else {
            $OK = $LS->insert($save);
            //插入不知道要不要的数据中
            $LS1 = Service('BookGet', 'sbook');
            unset($save['CREATETIME']);
            $save['SM_TIME'] = date("Y-m-d H:i:s");
            $save['IS_BLACK'] =$_POST['IS_BLACK'];
            $OK1 = $LS1->insert($save);
            $data['OK'] = $OK;
        }
        $DATA = ResData($data);
        $this->ajaxReturn($DATA, 'JSON');
    }

    /*
     * 审核不通过
     * 用户操作备注
     */
    public function UserAddOrderBookInfo() {
        $LS = Service('BookGet', 'order_book');
        $where['ob_id'] = $_POST['ob_id'];
        $obData = $LS->find($where);
        $save['info'] = $obData['info'] . ' ' . $_POST['info'];
        $data = $LS->update($where, $save);
        //如果书籍随订单寄回，记录用户操作
        $save['OLD_OB_ID'] = $_POST['ob_id'];
        $save['CREATE_TIME'] = date('Y-m-d H:i:s');
        $save['OVERDUE_TIME'] = date('Y-m-d H:i:s', time() + 30 * 24 * 60 * 60);;
        $save['STATE'] = $_POST['type'];
        $save['USER_ID'] = passport_decrypt($_POST['token'], C('JM_KEY'));
        $TLS = Service('M', 'next_order');
        $TLS->Add($save);
        $DATA = ResData($data);
        $this->ajaxReturn($DATA, 'JSON');
    }

    /*
     * 获取用户之前扫码没有的书籍，现在平台有的书籍
     */
    public function GetBookInShBook() {
        if($_POST['token']) {
            $where['ts_sh_book.OPENID'] = passport_decrypt($_POST['token'], C('JM_KEY'));
            $where['ts_sh_book.BOOK_ID'] = array('EXP',
                'IS NOT NULL');
            $LS = Service('M', 'sh_book');
            $join = 'ts_book on ts_book.ID = ts_sh_book.BOOK_ID';
            $files = 'ts_book.*';
            $data = $LS->join($where, $join, $files);
        }else{
            $data=array();
        }
        $DATA = ResData($data);
        $this->ajaxReturn($DATA, 'JSON');
    }

    /*
     * 获取用户之前扫码没有的书籍，现在平台有的书籍
     * 后删除
     */
    public function GetBookInShBook_Del() {
        $where['ts_sh_book.OPENID'] = passport_decrypt($_POST['token'], C('JM_KEY'));
        $where['ts_sh_book.BOOK_ID'] = array('EXP',
            'IS NOT NULL');
        $LS = Service('M', 'sh_book');
        $data = $LS->Del($where);
        $DATA = ResData($data);
        $this->ajaxReturn($DATA, 'JSON');
    }


    /*
 * 用户回收订单
 * 管理员收到通知
 * order_id 订单id
 */

    public function AddOrder_SendMsg($order_id, $user_name, $sm_time, $adderss, $phone) {
        $APPID = C('WX_APPID'); //微信公共号appid
        $SECRETID = C('WX_SECRET');//微信公共号 SECRETID
        $template_id = '5kwb7VBTEHKpbOz_HU4PYTBl8uFnJWCyBt3ixjivbx4';//消息模板id
        $user = C('ADMIN_OPENID');
        //消息体信息
        $first = '有人卖书，快去叫快递';
        $keyword1 = $user_name;
        $keyword2 = $sm_time;
        $keyword3 = $adderss;
        $keyword4 = '收书';
        $keyword5 = $phone;
        $remark = '-->点击查看详情吧o(∩_∩)o ';
        $url = 'http://www.yanxiang.pw/sell/admin?oid=' . $order_id;//链接
        $Send = new \Util\WxSendMsg\SendMsg();
        $data = array($keyword1,
            $keyword2,
            $keyword3,
            $keyword4,
            $keyword5);
        foreach ($user as $k => $v) {
            $Send->init($APPID, $SECRETID, $v, $template_id, $url);
            $Send->Setfirst($first);
            $Send->Setremark($remark);
            $Send->Setdata($data);
            $OK = $Send->SendMsg();
        }
    }

    /*
     * 回收订单平台不要书籍
     * 用户处理操作
     */
    public function HSOrderUserCl(){
        $LS = Service('M','order_book_user_cl');
        $save = $_POST;
        $save['CREATE_TIME'] = date('Y-m-d h:i:s');
        $save['USER_ID'] =  passport_decrypt($_POST['token'], C('JM_KEY'));
        $OK = $LS->Add($save);
        $data['OK'] =  $OK;
        $DATA = ResData($data);
        $this->ajaxReturn($DATA, 'JSON');
    }


    /*
     * 回收书籍拼团收益明细
     *
     */
    public function HSGroupOrderSYINFO(){
        $OLS = Service('M','group_order_log');
        $Owhere['ORDER_ID'] = $_POST['ORDER_ID'];
        $ODATA =  $OLS->find($Owhere);
        $LS = Service('M', 'hs_group_order_view');
        $where['group_id'] = $ODATA['GROUP_ID'];
        $data = $LS->query($where);
        $this->ajaxReturn($data, 'JSON');
    }

//    public function test() {
//        $where['ts_order.order_id'] = 'WXHSOID_5a7d556bdd88c95';
//        $LS = Service('BookGet', 'order');
//        //取消订单，查看是否参团，是删除进团记录，如果是该团的最后一个则删除团
//        $save['order_type'] = 6;
//        $data = $LS->qxOrder($where, $save);
//        dump($data);
//    }

}